<?php
/**
 * Class that operate on table 'tag'. Database Mysql.
 *
 * @version    $Id: TagMySqlDAO.class.php 11 2010-04-27 15:38:41Z richardjohnjensen@gmail.com $ 
 * @author: ${author} 
 * @date: 2010-04-08 12:18 
 */
class TagMySqlDAO implements TagDAO
{

	/**
	 * Get Domain object by primry key
	 *
	 * @param String $id primary key
	 * @return TagMySql 
	 */
	public function load($id) {
		$sql = 'SELECT * FROM tag WHERE tag = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($id);
		return $this->getRow($sqlQuery);
	}

	/**
	 * Get all records from table
	 */
	public function queryAll() {
		$sql = 'SELECT * FROM tag';
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Get all records from table ordered by field
	 *
	 * @param $orderColumn column name
	 */
	public function queryAllOrderBy($orderColumn) {
		$sql = 'SELECT * FROM tag ORDER BY ' . $orderColumn;
		$sqlQuery = new SqlQuery($sql);
		return $this->getList($sqlQuery);
	}
	
	/**
	 * Delete record from table
	 * @param tag primary key
	 */
	public function delete($tag) {
		$sql = 'DELETE FROM tag WHERE tag = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($tag);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
	 * Insert record to table
	 *
	 * @param TagMySql tag
	 */
	public function insert($tag) {
		$sql = 'INSERT INTO tag (template, label, updated) VALUES (?, ?, ?)';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($tag->template);
		$sqlQuery->set($tag->label);
		$sqlQuery->set($tag->updated);

		$id = $this->executeInsert($sqlQuery);	
		$tag->tag = $id;
		return $id;
	}
	
	/**
	 * Update record in table
	 *
	 * @param TagMySql tag
	 */
	public function update($tag) {
		$sql = 'UPDATE tag SET template = ?, label = ?, updated = ? WHERE tag = ?';
		$sqlQuery = new SqlQuery($sql);
		
		$sqlQuery->set($tag->template);
		$sqlQuery->set($tag->label);
		$sqlQuery->set($tag->updated);

		$sqlQuery->set($tag->tag);
		return $this->executeUpdate($sqlQuery);
	}
	
	/**
	 * Delete all rows
	 */
	public function clean() {
		$sql = 'DELETE FROM tag';
		$sqlQuery = new SqlQuery($sql);
		return $this->executeUpdate($sqlQuery);
	}

	public function queryByTemplate($value) {
		$sql = 'SELECT * FROM tag WHERE template = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByLabel($value) {
		$sql = 'SELECT * FROM tag WHERE label = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}

	public function queryByUpdated($value) {
		$sql = 'SELECT * FROM tag WHERE updated = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->getList($sqlQuery);
	}


	public function deleteByTemplate($value) {
		$sql = 'DELETE FROM tag WHERE template = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByLabel($value) {
		$sql = 'DELETE FROM tag WHERE label = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}

	public function deleteByUpdated($value) {
		$sql = 'DELETE FROM tag WHERE updated = ?';
		$sqlQuery = new SqlQuery($sql);
		$sqlQuery->set($value);
		return $this->executeUpdate($sqlQuery);
	}


	
	/**
	 * Read row
	 *
	 * @return TagMySql 
	 */
	protected function readRow($row) {
		$tag = new Tag();
		
		$tag->tag = $row['tag'];
		$tag->template = $row['template'];
		$tag->label = $row['label'];
		$tag->updated = $row['updated'];

		return $tag;
	}
	
	protected function getList($sqlQuery) {
		$tab = QueryExecutor::execute($sqlQuery);
		$ret = array();
		for($i=0;$i<count($tab);$i++) {
			$ret[$i] = $this->readRow($tab[$i]);
		}
		return $ret;
	}
	
	/**
	 * Get row
	 *
	 * @return TagMySql 
	 */
	protected function getRow($sqlQuery) {
		$tab = QueryExecutor::execute($sqlQuery);
		if (count($tab) == 0) {
			return null;
		}
		return $this->readRow($tab[0]);		
	}
	
	/**
	 * Execute sql query
	 */
	protected function execute($sqlQuery) {
		return QueryExecutor::execute($sqlQuery);
	}
		
	/**
	 * Execute sql query
	 */
	protected function executeUpdate($sqlQuery) {
		return QueryExecutor::executeUpdate($sqlQuery);
	}

	/**
	 * Query for one row and one column
	 */
	protected function querySingleResult($sqlQuery) {
		return QueryExecutor::queryForString($sqlQuery);
	}

	/**
	 * Insert row to table
	 */
	protected function executeInsert($sqlQuery) {
		return QueryExecutor::executeInsert($sqlQuery);
	}
}
?>